package capricornus.components.utils.tipmotion
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	public class TipMotionController extends EventDispatcher
	{
		private var target:DisplayObject;
		
		private var currentState:int = 0;
		
		private var beginValue:Number;
		private var t1:Number = 0.3;
		private var duration:Number = 1200;
		private var t2:Number = 0.6;
		private var h1:Number = 20;
		private var h2:Number = 30;
		
		private static var _dict:Dictionary = new Dictionary();
		
		private var _remove:Boolean = false;
		
		public var onComplete:Function = null;
		
		private var timer:Timer = null;
		
		/**
		 * 提示动画 ；时间单位都是秒
		 * @param target
		 * @param h1  向上第一段距离；或者透明值(move=false)
		 * @param h2 向上第二段距离；或者透明值(move=false)
		 * @param t1 第一段时间
		 * @param duration 第一段到第二段的中间持续时间
		 * @param t2 第二段时间
		 * @param move true时为向上移动，false是透明值变化
		 * 
		 */		
		public function TipMotionController( target:DisplayObject, h1:Number=20, h2:Number=30, t1:Number=0.3, duration:Number=1.2, t2:Number=0.6 )
		{
			this.h1 = h1;
			this.h2 = h2;
			this.t1 = t1;
			this.t2 = t2;
			this.duration = duration * 1000;
			this.target = target;
			init();
		}
		
		public function set removeOnComplete( value:Boolean ):void
		{
			_remove = value;
		}
		
		private function init():void
		{
			currentState = 0;
			
			target.alpha = 0;
			var obj:Object = {};
			obj.alpha = 1;
			obj.y = target.y-h1;
			obj.onComplete = onComplete1;
			TweenLite.to( target, t1, obj );
			
			_dict[this] = this;
		}
		
		public function destroy():void
		{
			dispatchEvent( new Event( "destroy" ) );
			if( timer != null )
			{
				if( timer.running )
				{
					timer.stop();
				}
				timer.removeEventListener(TimerEvent.TIMER, onTimer );
				timer = null;
			}
			if( _dict[this] )
			{
				delete _dict[this];
			}
			
			if( _remove )
			{
				if( target && target.parent )
				{
					target.parent.removeChild( target );
				}
			}
			target = null;
		}
		
		private function onComplete1():void
		{
			if( timer==null )
			{
				timer = new Timer( duration, 1 );
				timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true );
			}
			timer.reset();
			timer.start();
		}
		
		private function onComplete2():void
		{
			if( onComplete != null )
			{
				onComplete( target );
				onComplete = null;
			}
			destroy();
		}
		
		private function onTimer( e:TimerEvent ):void
		{
			disappear();
		}
		
		private function disappear():void
		{
			if( target != null )
			{
				var obj:Object = {};
				obj.alpha = 0;
				obj.y = target.y-h2;
				obj.onComplete = onComplete2;
				TweenLite.to( target, t2, obj );
			}
		}
	}
}















